﻿using System;
using System.Collections.Generic;
using System.Text;
using SecurityDAL;
using System.Data;
using Model.Security.FindEntityInfo;
using MyExection;
using Model.Security.EntityInfo;
using System.Data.SqlClient;
using BusinessLogicDAL;
using Model.BusinessLogic.FindEntityInfo;
using Model.BusinessLogic.EntityInfo;

namespace BusinessLogicBLL
{
    public class RenYuanRenWuBLL
    {
        RenYuanRenWuDAL renYuanRenWuDAL = null;
        UserDAL userDAL = null;
        public RenYuanRenWuBLL()
        {
            renYuanRenWuDAL = new RenYuanRenWuDAL();
            userDAL = new UserDAL();
        }

        /// <summary>
        /// 创建人：
        /// 创建时间：2010-7-12
        /// 功能介绍：查询DataTable信息
        /// </summary>
        /// <param name="FindEntity  findEntity"></param>
        /// <returns></returns>
        public DataTable SearchByFindEntity(FindRenYuanRenWuEntity findEntity)
        {
            try
            {
                return renYuanRenWuDAL.SearchByFindEntity(findEntity);
            }
            catch (CustomException)
            {
                throw new CustomException("查询DataTable信息出错，请重试！");
            }
        }

        /// <summary>
        /// 创建时间：2010-7-12
        /// 功能介绍：添加RenYuanRenWuEntity
        /// </summary>
        /// <param name="entityInfo"></param>
        /// <returns></returns>
        public bool Add(List<RenYuanRenWuEntity> entityList)
        {
            bool flag = false;
            using (SqlConnection con = new SqlConnection(DBUtility.DBSQLHelper.defaultConnectionString))
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlTransaction trans = con.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                try
                {
                    int index = 0;
                    foreach (RenYuanRenWuEntity entity in entityList)
                    {
                        if (index == 0)
                        {
                            renYuanRenWuDAL.Del_RenYuanRenWuByNianFen(trans, entity);
                        }
                        flag = renYuanRenWuDAL.Add(trans, entity);
                        if (!flag)
                        {
                            trans.Rollback();
                            return flag;
                        }
                        index++;
                    }
                    trans.Commit();
                }
                catch
                {
                    trans.Rollback();
                    return false;
                }
            }
            return flag;
        }
        /// <summary>
        /// 创建人：
        /// 创建时间：2010-7-12
        /// 功能介绍：更新RenYuanRenWuEntity
        /// </summary> 
        /// <param name="entityInfo"></param>
        /// <returns></returns>      
        public bool Update(RenYuanRenWuEntity entityInfo)
        {
            try
            {
                return renYuanRenWuDAL.Update(entityInfo);
            }
            catch (CustomException)
            {
                throw new CustomException("更新RenYuanRenWuEntity出错，请重试！");
            }
        }

        /// <summary>
        /// 创建人：
        /// 创建时间：2010-7-12
        /// 功能介绍：根据“key”查询RenYuanRenWuEntity
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public RenYuanRenWuEntity GetListDetailById(string key)
        {
            try
            {
                return renYuanRenWuDAL.GetListDetailById(key);
            }
            catch (CustomException)
            {
                throw new CustomException("根据“key”查询RenYuanRenWuEntity出错，请重试！");
            }
        }
        /// <summary>
        /// 获取有效人员信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllUserForYouXiao()
        {
            try
            {
                return renYuanRenWuDAL.GetAllUserForYouXiao();
            }
            catch (CustomException)
            {
                throw new CustomException("获取有效人员信息出错，请重试！");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="RY_ID"></param>
        /// <returns></returns>
        public User GetRenYuanEntity(int RY_ID)
        {
            try
            {
                return userDAL.GetListDetailById(RY_ID.ToString());
            }
            catch (CustomException)
            {
                throw new CustomException("获取人员信息出错，请重试！");
            }
        }
        /// <summary>
        /// 功能介绍：获取人员任务根据年份
        /// </summary>
        /// <param name="nianFen"></param>
        /// <returns></returns>
        public DataTable GetRenYuanRenWuByNianFen(string nianFen)
        {

            try
            {
                return renYuanRenWuDAL.GetRenYuanRenWuByNianFen(nianFen);
            }
            catch (CustomException)
            {
                throw new CustomException("获取人员信息出错，请重试！");
            }
        }
    }
}
